package com.talkweb.newproject.admin.controller;

import com.talkweb.newproject.admin.rest.SysOrgRest;
import com.talkweb.newproject.admin.sysorg.dto.SysOrgDTO;
import com.talkweb.newproject.api.model.admin.TreeNodeVo;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qsf on 2019/5/13.
 */
@RestController
@RequestMapping(value="/sysorg-api")
@Slf4j
public class SysOrgController {
    @Autowired
    private SysOrgRest orgService;

    /**
     * 新增数据
     * @param dto
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/addOrg",method = RequestMethod.POST)
    public ResponseResult<SysOrgDTO> addOrg(@RequestBody SysOrgDTO dto) throws Exception {
        ResponseResult<SysOrgDTO> result = new ResponseResult<>();
        try{
            if(dto != null && dto.getId()>0) {
                result = orgService.update(dto);
            }else {
                result = orgService.create(dto);
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @RequestMapping(value="/querySysOrgList",method = RequestMethod.POST)
    public ResponseResult<List<TreeNodeVo>> querySysOrgList(@RequestBody SysOrgDTO dto) throws Exception {
        ResponseResult<List<SysOrgDTO>> sysOrgList = new ResponseResult<>();
        ResponseResult<List<TreeNodeVo>> result = new ResponseResult();
        try{

            sysOrgList = orgService.querySysOrgList(dto);
            result.setData(this.toTreeModel(sysOrgList.getData()));
            result.setStatus(Status.SUCCESS);

            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    private List<TreeNodeVo> toTreeModel(List<SysOrgDTO> data) {
        List<TreeNodeVo> treeNodeVoList = new ArrayList<>();
        for(SysOrgDTO dto: data) {
            TreeNodeVo vo = new TreeNodeVo();
            vo.setId(dto.getId().toString());
            vo.setName(dto.getOrgName());
            vo.setParentId(dto.getParentId());
            vo.setLevel(dto.getOrgLevel());
            vo.setOrgType(dto.getOrgType());
            vo.setValidateState(dto.getValidateState());

            treeNodeVoList.add(vo);
        }
        return TreeNodeVo.treeBuild(treeNodeVoList);
    }

    @ApiOperation(value="根据主键id获取机构信息",notes = "返回结果:SUCCESS:200;FAILED:500;",httpMethod = "POST")
    @RequestMapping(value="/querySysOrg",method = RequestMethod.POST)
    public ResponseResult<SysOrgDTO> querySysOrg(@RequestParam("") String orgid) throws Exception {
        ResponseResult<SysOrgDTO> result= new ResponseResult<>();
        try{
            result = orgService.querySysOrgByPrimaryKey(orgid);
            result.setStatus(Status.SUCCESS);
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    @RequestMapping(value="/deleteSysOrg",method = RequestMethod.POST)
    public ResponseResult deleteSysOrg(@RequestParam("") String orgid) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            result = orgService.deleteOrgById(orgid);
            result.setStatus(Status.SUCCESS);
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }
}
